# 42. configparser模块 - java配置文件模块

# configparser模块

configparser模块是什么:

  1. 有一种固定格式的配置文件
  2. 有一个对应的模块去帮你做这个文件的字符串处理
  3. 该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)

configparser模块一般不常用,目前的项目都不用java语言的配置文件格式,所以configparser模块也基本没人用,如果有人用,那这些人基本都是跟java程序需要关联的人

因为这个模块基本没人用了,所以接下来的操作实验,我就直接简单化

# 创建配置文件

import configparser
config = configparser.ConfigParser()
##实例化configparser模块中的ConfigParser类

config["DEFAULT"] = {'ServerAliveInterval': '45',
                      'Compression': 'yes',
                     'CompressionLevel': '9',
                     'ForwardX11':'yes'
                     }
config['bitbucket.org'] = {'User':'hg'}
config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}
## 创建配置文件的组跟项

with open('example.ini', 'w') as f:
   config.write(f)
## 把定义好的组跟项,按configparser模块中的write写入方法写入到文件中


## 生成后的配置文件
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = hg

[topsecret.server.com]
host port = 50022
forwardx11 = no

## []中的:组
## []下面的:项,组的选项
## [DEFAULT]这个组是一个特殊的组,代表全局组,就是说这个组在其他组中可以调用这个组的项

# 查找使用操作

查找文件内容,基于字典的形式

想要查看文件,就要先把文件打开后才能看

## 配置文件example.ini
## 在上面有生成的方法,和生成后的配置项

## 导入模块,实例化configparser模块中的ConfigParser类
import configparser
config = configparser.ConfigParser()

====================================================================================

## 打开文件或查看
config.read('example.ini')
print(config.sections())
执行结果:
['bitbucket.org', 'topsecret.server.com']
## 那为什么没有[DEFAULT]这个组,因为这个组是全局的,在其他组都可以调用他

====================================================================================

## 查看指定的组名是否存在
config.read('example.ini')
print('bytebong.com' in config)
print('bitbucket.org' in config)
执行结果:
False
True

====================================================================================

## 查看组中的项的值
config.read('example.ini')
print(config['bitbucket.org']["user"])
print(config['DEFAULT']['Compression'])
print(config['topsecret.server.com']['ForwardX11'])
执行结果:
hg
yes
no

====================================================================================

## 使用for循环查看指定组的所有项
## 可以迭代方式来读取查询
config.read('example.ini')
print(config['bitbucket.org'])  
执行结果:
<Section: bitbucket.org>

## 上面一个执行后的结果是一个内存地址,那可以不可以迭代,直接for循环试试
for key in config['bitbucket.org']:
    print(key)
执行结果:
user
serveraliveinterval
compression
compressionlevel
forwardx11
# 为什么有DEFAULT组中的项,因为这项是全局的,所以在查看组中的所有内容时,DEFAULT组的项也会一起出现

====================================================================================

## 查看指定组中的所有项
config.read('example.ini')
print(config.options('bitbucket.org'))
执行结果:
['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']

====================================================================================

## 查看指定组中的所有项跟项值
config.read('example.ini')
print(config.items('bitbucket.org'))
执行结果:
[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]

====================================================================================

## get方法Section下的key对应的value
config.read('example.ini')
print(config.get('bitbucket.org','compression'))
执行结果:
yes

# 增删改操作

## 配置文件example.ini
## 在上面有生成的方法,和生成后的配置项

## 导入模块,实例化configparser模块中的ConfigParser类
import configparser
config = configparser.ConfigParser()

====================================================================================

## 新增组
config.read('example.ini')
config.add_section('yuan')
config.write(open('new2.ini', "w"))  ## 复制当前配置文件中的所有配置或生成另一个文件,将增加的修改操作在新文件执行
config.write(open('example.ini', "w")) ## 增加操作在当前的配置文件中

====================================================================================

## 删除
config.read('example.ini')
config.remove_section('bitbucket.org')  ## 删除整个组,删除组就会把组下的所有项全部删除
config.remove_option('topsecret.server.com',"forwardx11") ## 删除指定组的指定项
config.write(open('new2.ini', "w")) ## 复制当前配置文件中的所有配置或生成另一个文件,将删除的修改操作在新文件执行
config.write(open('example.ini', "w")) ## 删除操作在当前的配置文件中

====================================================================================

## 修改或增加
## 执行修改,如果要修改的项不存在,就会直接新增一个
config.read('example.ini')
config.set('topsecret.server.com','forwardx11','yes')  ## 指定组中的某个项的项 == yes ,修改
config.set('yuan','k2','22222') ##指定组中的某个项的项 == 22222 ,增加,因为该项不存在
config.write(open('new2.ini', "w")) ## 复制当前配置文件中的所有配置或生成另一个文件,将修改或增加的修改操作在新文件执行
config.write(open('example.ini', "w")) ## 修改或增加操作在当前的配置文件中